<?php 
class Node extends MY_Admin_Controller 
{

	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	/**
	 * 节点首页
	 */
	public function index()
	{
		$node_list = array();
		$query = $this->db->query("SELECT * FROM sk_rbac_node ORDER BY dirc,cont,func");
		$data = $query->result();
		
		foreach($data as $vo){
			$node_list[$vo->dirc][$vo->cont][$vo->func] = $vo;
		}
		$this->load->view('admin/rbac/node', array('node'=>$node_list));
	}
	
	/**
	 * 新增节点
	 * @param string $dirc
	 * @param string $cont
	 * @param string $func
	 */
	public function add($dirc=NULL,$cont=NULL,$func=NULL)
	{
		if($this->input->post()){
			$dirc = $this->input->post("dirc")?$this->input->post("dirc"):$dirc;
			$cont = $this->input->post("cont")?$this->input->post("cont"):$cont;
			$func    = $this->input->post("func");
			$memo   = $this->input->post("memo");
			$status   = $this->input->post("status")==1?1:0;
			if($dirc&&$cont&&$func&&$memo){
				$query = $this->db->query("SELECT id FROM sk_rbac_node WHERE dirc = '".$dirc."' AND cont = '".$cont."' AND func = '".$func."'");
				$data = $query->row_array();
				if(!$data){
					$sql = "INSERT INTO sk_rbac_node (`dirc`,`cont`,`func`,`status`,`memo`) values('{$dirc}','{$cont}','{$func}','{$status}','{$memo}')";
					//echo $sql;die();
					$this->db->query($sql);
					success_redirct('/rbac/node/index','节点添加成功！');
				}else{
					error_redirct('',"该节点已存在！");
				}
			}else{
				error_redirct('',"信息填写不全！");
			}
		}
		$this->load->view('admin/rbac/node/add',array('dirc'=>$dirc,'cont'=>$cont,'func'=>$func));
	}
	
	/**
	 * 删除节点
	 * @param string $dirc
	 * @param string $cont
	 * @param string $func
	 */
	public function delete($dirc=NULL,$cont=NULL,$func=NULL)
	{
		if($dirc==NULL){error_redirct("/rbac/node/index","操作失败");}
		
		if($this->input->post()){
			$verfiy = $this->input->post("verfiy");
			if($verfiy){
				$where_dirc = "dirc = '{$dirc}'";
				$where_cont = $cont==NULL?"":" AND cont = '{$cont}'";
				$where_func = $func==NULL?"":" AND func = '{$func}'";
				$query = $this->db->query("SELECT GROUP_CONCAT(id) as node_id FROM sk_rbac_node WHERE {$where_dirc} {$where_cont} {$where_func}");
				$node_list = $query->row_array();
				
				$sql = "DELETE FROM sk_rbac_node WHERE {$where_dirc} {$where_cont} {$where_func} ";
				$this->db->query($sql);
				success_redirct("/rbac/node/index","删除成功");
			}else{
				error_redirct("/rbac/node/index","操作失败");
			}

		}
		$this->load->view('admin/rbac/node/delete',array('dirc'=>$dirc,'cont'=>$cont,'func'=>$func));
	}
	
	/**
	 * 修改节点
	 * @param unknown $id
	 */
	public function edit($id)
	{
		$query = $this->db->query("SELECT * FROM sk_rbac_node WHERE id = ".$id);
		$data = $query->row_array();
		if($data){
			if($this->input->post()){
				$memo   = $this->input->post("memo");
				$status   = $this->input->post("status")==1?1:0;
				if($memo){
					$sql = "UPDATE sk_rbac_node set `memo`='{$memo}',`status` = '{$status}' WHERE id = {$id}";
					$this->db->query($sql);
					success_redirct("/rbac/node/index","节点修改成功");
				}else{
					error_redirct('',"信息填写不全！");
				}
			}
			$this->load->view("admin/rbac/node/edit",array('data'=>$data));
		}else{
			error_redirct("/rbac/node/index","未找到此节点");
		}
	}
	
	/**
	 * 批量导入节点
	 * @return [type] [description]
	 */
	public function importNode($dirc=NULL,$cont=NULL)
	{
		$cont_arr = array();
		
		if($cont==NULL)
		{
			foreach (glob(APPPATH."controllers/admin/".$dirc."/*.php") as $filename)
			{
				if($filename)
				{
					$filename = explode('/', $filename);
					$filename = end($filename);
					$filename = substr($filename, 0 , -4);
					$cont_arr[] = $filename;
				}
			}
		}else{
			$cont_arr[] = $cont;
		}
		
		$methods_name = array(
				'index'=>'列表页面',
				'lists'=>'列表页面',
				'add'=>'添加页面',
				'create'=>'添加页面',
				'insert'=>'添加操作',
				'save'=>'保存操作',
				'edit'=>'编辑页面',
				'update'=>'更新操作',
				'detail'=>'详情页面',
				'delete'=>'删除操作',
				'search'=>'搜索',
		);
		
		$sys_methods = array('__construct','isLogin','rbac','page_config','json_output','lib','isPOST','isAjax','cache_view','HTTPLastModified','uploadFile','get_instance');
		
		foreach($cont_arr as $cont)
		{
			include_once APPPATH."controllers/admin/".$dirc."/".$cont.".php";
			$methods = get_class_methods($cont);
			
			foreach($methods as $func){
				if(!in_array($func, $sys_methods))
				{
					$memo = isset($methods_name[$func])?$methods_name[$func]:$func;
					
					$query = $this->db->query("SELECT id FROM sk_rbac_node WHERE dirc = '".$dirc."' AND cont = '".$cont."' AND func = '".$func."'");
					$data = $query->row_array();
					if(!$data){
						$sql = "INSERT INTO sk_rbac_node (`dirc`,`cont`,`func`,`status`,`memo`) values('{$dirc}','{$cont}','{$func}','1','{$memo}')";
						$this->db->query($sql);
					}
				}
			}
		}
		
		success_redirct("/rbac/node/index", "节点导入成功");
	}
}
?>